package com.iswoak.netctoss.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.iswoak.netctoss.entity.RoleInfo;
import com.iswoak.netctoss.entity.RoleInfoVo;
import com.iswoak.netctoss.service.ModuleInfoService;
import com.iswoak.netctoss.service.RoleInfoService;
import com.iswoak.netctoss.service.impl.ModuleInfoServiceImpl;
import com.iswoak.netctoss.service.impl.RoleInfoServiceImpl;
import com.iswoak.netctoss.util.PageUtil;

@WebServlet("/role/*")
public class RoleServlet extends HttpServlet {
	
	RoleInfoService roleInfoService = new RoleInfoServiceImpl();
	
	ModuleInfoService moduleInfoService = new ModuleInfoServiceImpl();
	
	protected void service(HttpServletRequest req, 
			HttpServletResponse resp) throws ServletException, IOException {
		//设置
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String uri = req.getRequestURI();
		if(uri.endsWith("/list")) {
			list(req,resp);
		} else if(uri.endsWith("/toAddJsp")) {
			toAdd(req,resp);
		} else if(uri.endsWith("/ajaxCheckName")) {
			ajaxCheckName(req,resp);
		} else if(uri.endsWith("/add")){
			add(req,resp);
		} else if(uri.endsWith("/toModiJsp")) {
			toModiJsp(req,resp);
		} else if (uri.endsWith("/update")){
			update(req,resp);
		}
		
		
	}

	// 修改
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String roleId = req.getParameter("roleId");
		String roleName = req.getParameter("roleName");
		//创建角色对象
		RoleInfo roleInfo = new RoleInfo(Integer.valueOf(roleId), roleName);
		String[] arrStr = req.getParameterValues("moduleId");
		Integer[] moduleId = new Integer[arrStr.length];
		for(int i = 0; i<arrStr.length; i++) {
			moduleId[i] = Integer.valueOf(arrStr[i]);
		}
		//执行修改操作
		roleInfoService.update(roleInfo, moduleId);		
		PrintWriter pw = resp.getWriter();
		pw.println("success");
		pw.close();
		
		
	}
	
	// 跳转到修改页
	private void toModiJsp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取角色id
		Integer roleId = Integer.valueOf(req.getParameter("roleId"));
		//根据角色id查询角色信息
		req.setAttribute("roleInfoVo", roleInfoService.findByRoleId(roleId));
		//查询所有模块
		req.setAttribute("moduleInfos", moduleInfoService.findAll());
		//转发
		req.getRequestDispatcher("/jsp/role/role_modi.jsp").forward(req, resp);
	}

	/**
	 * 分页查询 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest req, 
			HttpServletResponse resp) throws ServletException, IOException {
		RoleInfoVo roleInfoVo = new RoleInfoVo();
		Integer currPage = null;//页数
		
		if(!StringUtils.isBlank(req.getParameter("currPage"))) {
			currPage = Integer.valueOf(req.getParameter("currPage"));
		} else {
			currPage = 1;
		}
		
		Integer pageSize = null;//每页条数
		if(!StringUtils.isBlank(req.getParameter("pageSize"))) {
			pageSize = Integer.valueOf(req.getParameter("pageSize"));
		} else {
			pageSize = 2;
		}
		
		PageUtil<RoleInfoVo> pageUtil = roleInfoService.page(currPage, pageSize);
		req.setAttribute("page", pageUtil);
		//模拟查询数据转发到角色查询页
		req.getRequestDispatcher("/jsp/role/role_list.jsp").forward(req, resp);
	}
	
	// 跳转到新增页
	private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("moduleInfos", moduleInfoService.findAll());
		req.getRequestDispatcher("/jsp/role/role_add.jsp").forward(req, resp);
	}
	
	/**
	 * 校验角色名是否重复
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	public void ajaxCheckName(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		String roleId = req.getParameter("roleId");//获取角色id
		String roleName = req.getParameter("roleName");//获取用户输入角色名
		RoleInfo role = null;
		if(StringUtils.isBlank(roleId)) {//新增中校验角色名
			role = roleInfoService.findByRoleName(roleName,null);
		} else {//修改中校验角色名
			role = roleInfoService.findByRoleName(roleName, roleId);
		}
		
		PrintWriter pw = resp.getWriter();
		if(role == null){
			//数据库中没有该roleName则代表可用
			pw.println("OK");
		}else{
			pw.println("ERROR");
		}
		pw.close();
	}
	
	/**
	 * 新增角色
	 * @param roleName
	 * @param moduleId
	 * @return
	 * @throws IOException 
	 */
	public void add(HttpServletRequest req,HttpServletResponse resp) throws IOException {
		String roleName = req.getParameter("roleName");
		String[] arrStr = req.getParameterValues("moduleId");
		Integer[] moduleId = new Integer[arrStr.length];
		for(int i = 0; i<arrStr.length; i++) {
			moduleId[i] = Integer.valueOf(arrStr[i]);
		}
		roleInfoService.add(roleName, moduleId);
		PrintWriter pw = resp.getWriter();
		pw.println("success");
		pw.close();
	}
	
}
